package com.leetcode.partition3;

import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2021/9/7 10:34
 */
public class LC242有效的字母异位词 {

    public static boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) return false;
        return hashtable(s, t);
    }

    public static void main(String[] args) {
        String s = "anagram";
        String t = "nagaram";
        System.out.println(isAnagram(s, t));
    }

    private static boolean hashtable(String s, String t) {
        int[] ht = new int[26];
        char[] arr1 = s.toCharArray(), arr2 = t.toCharArray();
        for (int i = 0; i < arr1.length; i++) ht[arr1[i] - 'a']++;
        for (int i = 0; i < arr2.length; i++) ht[arr2[i] - 'a']--;

        for (int i = 0; i < ht.length; i++) {
            if (ht[i] != 0) return false;
        }
        return true;
    }

    private static boolean sorted(String s, String t) {
        char[] arr1 = s.toCharArray(), arr2 = t.toCharArray();
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) return false;
        }
        return true;
    }
}
